var woDetail = function() {
	var validator;
	return {
		init : function(woId,woType) {
			var tableId = "woHistory_table";
			getWordOrderDetail(woId,woType);

			$('.init-radio-style').on('click',function() {
    			var $this=$(this);
    			$('.init-radio-style').removeClass('on');
    			$this.addClass("on");
			});
			//查询字典信息列表
			function queryData(sSource, aoData, fnCallback) {
				aoData.push({
					"name" : "woId",
					"value" : woId
				});
				$.ajax({
					"type" : "post",
					"url" : sSource,
					"dataType" : "json",
					"data" : {
						"aoData" : JSON.stringify(aoData)
					},
					"success" : function(resp) {
						fnCallback(resp);
					}
				});
			};

			$('#woHistory_table').DataTable({
				"processing" : true,
				"serverSide" : true,
				"sAjaxSource" : "main/wo/getWorkOrderHistory.mvc",
				"fnServerData" : queryData,
				"pageLength" : 10,
				"sDom" : "<'row-fluid'r>t<'row-fluid'<'span6'i><'span6'p>>",
				"oLanguage" : util.getDToLanguage(),
				"columns" : [ 
				{	
					"data":"index",//序号
					"bSortable" : false,
            		"render":function(data,type,row,meta){
            			var startIndex = meta.settings._iDisplayStart;
             			return startIndex+meta.row+1;
         			}
				},
				{
					"data" : "operatingDateStr",
					"bSortable" : false,
					"defaultContent": ""
				},
				{
					"data" : "operator",
					"bSortable" : false,
					"defaultContent": ""
				},
				{
					"data" : "opDescription",
					"bSortable" : false,
					"render" : function(data, type, full, meta) {
						if("0"==data){
							return "生成服务工单。";
						} else if("1"==data){
							return "修改工单状态为:未处理。";
						} else if("2"==data) {
							return "修改工单状态为:已确认。";
						} else if("3"==data) {
							return "修改工单状态为:处理中。";
						} else if("4"==data) {
							return "修改工单状态为:已解决。";
						} else if("5"==data) {
							return "修改工单状态为:已关闭。";
						} else if(full.opType == "2") {
							return "上传文件: "+data;
						} else if(full.opType == "3") {
							return "删除文件: "+data;
						}
					},
					"defaultContent": ""
				}
				],
				"fnDrawCallback" : function(oSetting) {
					util.clearSelected(tableId);
				}
			});

			$('#file_table').DataTable({
				"processing" : true,
				"serverSide" : true,
				"sAjaxSource" : "main/wo/getWorkOrderfile.mvc",
				"fnServerData" : queryData,
				"pageLength" : 10,
				"sDom" : "<'row-fluid'r>t<'row-fluid'<'span6'i><'span6'p>>",
				"oLanguage" : util.getDToLanguage(),
				"columns" : [ 
				{	
					"data":"index",//序号
					"bSortable" : false,
            		"render":function(data,type,row,meta){
            			var startIndex = meta.settings._iDisplayStart;
             			return startIndex+meta.row+1;
         			}
				},
				{
					"data" : "originalFileName",
					"bSortable" : false,
					"defaultContent": ""
				},
				{
					"data" : "fileSize",
					"bSortable" : false,
					"render" : function(data, type, full, meta) {
						 return parseInt(data/1024)+" Kb";
					}
				},
				{
					"data" : "createDateStr",
					"bSortable" : false,
					"defaultContent": ""
				},
				{
					"bSortable" : false,
					"render" : function(data, type, full, meta) {
						 return "<a  data-pkid='"+full.pkId+"' class=\"download\" style=\"cursor:pointer;color:blue;\" >下载</a>";
					}
				},
				{
					"bSortable" : false,
					"render" : function(data, type, full, meta) {
						 return "<a  data-pkid='"+full.pkId+"'  class=\"delete\" style=\"cursor:pointer;color:blue;\" >删除</a>";
					}
				}
				],
				"fnDrawCallback" : function(oSetting) {
					util.clearSelected("file_table");
				}
			});

			//修改工单状态
			$("#btn_submit").on('click',  function(e) {
				e.preventDefault();
				var woStatus = $("input[name='woStatus']:checked").val();
				var table = $('#woHistory_table').DataTable();
				$.ajax({
					"type" : "post",
					"url" : "main/wo/updateWoStatus.mvc",
					"async" : true,
					"dataType" : "json",
					"data" : {
						"woId":woId,
						"woType":woType,
						"woStatus":woStatus
					},
					"success" : function(resp) {
						if(resp.msg == "SUCCESS") {
							rmsBox.notify('success', {msg: '工单状态已提交！'});
							table.draw(true);
						}else {
							rmsBox.notify('error', {msg: '提交工单状态失败！',delay:3000});
						}
					}
				});
			});

			$("#file_table_add").on('click', function(e) {
				e.preventDefault();
				$("#add-file").modal({show:true,keyboard:false});
			});

			//上传文件
			$("#form_file_save").on('click', function(e) {
				e.preventDefault();
				if(check()){
					//$("#woId").val(woId);
					//$("#file_form").submit();
					var table = $("#file_table").DataTable();
					var hisTable = $('#woHistory_table').DataTable();
					$.ajaxFileUpload({
			            url: 'main/wo/upload2.mvc', 
			            type: 'post',
			            secureuri: false, //一般设置为false
			            fileElementId: 'uploadfile', // 上传文件的id、name属性名
			            dataType: 'json', //返回值类型，一般设置为json、application/json
			            data : {
			            	"woId":woId
			            },
			            success: function(data, status){ 
			            	if(data.msg=='SUCCESS'){
								rmsBox.notify('success', {msg: '保存成功！'});
								$("#form_btn_close").click();
								table.draw(true);
								hisTable.draw(true);
							}else{
								rmsBox.notify('error', {msg: '保存失败！',delay:3000});
							}
			            },
			            error: function(data, status, e){ 
			                console.log(e);
			            }
	        		});
	        	}
			});

			$("#file_table").on('click', 'a.download', function(e) {
				e.preventDefault();
				var pkId = $(this).attr("data-pkId");
				$.fileDownload('main/wo/download.mvc', {
					httpMethod: 'POST',
					data: {"pkId":pkId},
					prepareCallback:function(url){
						
					},
					successCallback:function(url){
						//App.unblockUI($(loadingId));//停止loading
						//rmsBox.notify('success', {msg: '操作成功'});
					},
					failCallback: function (html, url) {
						//App.unblockUI($(loadingId));//停止loading
						rmsBox.notify('error', {msg: '下载失败！',delay:3000});
					}
				});
			});

			$("#file_table").on('click', 'a.delete', function(e) {
				e.preventDefault();
				util.showModal("#_confirmModal", "您确定要删除该文件？", "_confirmModal_yes");
				var pkId = $(this).attr("data-pkId");
				$("#_confirmModal_yes").attr('data-pkid', pkId);

				
			});

			$("#_confirmModal").on('click', '#_confirmModal_yes', function() {
				var pkId = $(this).attr("data-pkId");
				$(this).removeAttr('data-pkid');
				var table = $("#file_table").DataTable();
				var hisTable = $('#woHistory_table').DataTable();
				$.ajax({
					"type" : "post",
					"url" : "main/wo/deleteFile.mvc",
					"async" : true,
					"dataType" : "json",
					"data" : {
						"pkId":pkId
					},
					"success" : function(resp) {
						if(resp.msg == "SUCCESS") {
							rmsBox.notify('success', {msg: '文件删除成功！'});
							table.draw(true);
							hisTable.draw(true);
						}else {
							rmsBox.notify('error', {msg: '文件删除失败！',delay:3000});
						}
					}
				});
			});

			

			

			function getWordOrderDetail(woId, woType){
				$.ajax({
					"type" : "post",
					"url" : "main/wo/getWordOrderDetail.mvc",
					"async" : true,
					"dataType" : "json",
					"data" : {
						"woId":woId,
						"woType":woType
					},
					"success" : function(resp) {
						if (resp.woType == '2') {
							$("#woDetail").css('height', '530px');
							$("#serviceDate").show();
							$("#serviceStartDate").val(resp.serviceStartDate);
							$("#serviceEndDate").val(resp.serviceEndDate);
						} else if (resp.woType == '1') {
							$("#serviceDate").hide();
							$("#woDetail").css('height', '490px');
						}
						$("#woCode").val(resp.woCode);
						$("#woCategory").val(resp.woCategory);
						if("1" == resp.woImportance){
							$("#woImportance").val("一般");
						}else if("2" == resp.woImportance){
							$("#woImportance").val("中等");
						}else if("3" == resp.woImportance){
							$("#woImportance").val("紧急");
						}
						if("1" == resp.woType) {
							if("1"==resp.woCategory){
								$("#woCategory").val("报警处理");
							} else if("2"==resp.woCategory){
								$("#woCategory").val("故障解决");
							} else if("3"==resp.woCategory) {
								$("#woCategory").val("维保服务");
							} else if("4"==resp.woCategory) {
								$("#woCategory").val("培训服务");
							} else if("5"==resp.woCategory) {
								$("#woCategory").val("其他");
							} 
						}else if("2" == resp.woType) {
							if("1"==resp.woCategory){
								$("#woCategory").val("巡检计划");
							} else if("2"==resp.woCategory){
								$("#woCategory").val("维保计划");
							} else if("3"==resp.woCategory) {
								$("#woCategory").val("培训计划");
							} 
						}
						$("input:radio[value='"+resp.woStatus+"']").attr('checked', 'checked');
						$("#woSubmitter").val(resp.woSubmitter);
						$("#woCustomer").val(resp.woCustomer);
						$("#woContent").val(resp.woContent);
						$("#woDescription").val(resp.woDescription);
						$("#contacts").val(resp.contacts);
						$("#contactNumber").val(resp.contactNumber);
					}
				});

			};

			//检测文件类型
            function check(){
            	debugger
            	var maxsize = 2*1024;
            	var filetypes =["jpg","jpeg","png","rar","txt","zip","doc","ppt","pptx","xls","pdf","docx","xlsx","xml"]; 
                var file = $("#uploadfile").val();
                console.log("文件路径："+file);
                if(file == '' || file == null) {
                    rmsBox.notify('warning', {msg: '请选择文件！', delay:3000});
                    return false;
                }
                
                var index = file.lastIndexOf(".");
                var ext = file.substring(index + 1, file.length).toLowerCase();
                console.log("文件类型："+ext);
                if($.inArray(ext,filetypes)==-1) {
                    rmsBox.notify('warning', {msg: '不允许上传'+ext+'格式的文件！', delay:3000});
                    return false;
                } 
                
                //检测文件大小
                
                var obj =  document.getElementById("uploadfile");
                var fileSize = 0;
                var isIE = /msie/i.test(navigator.userAgent) && !window.opera;            
                if (isIE && !obj.files) {          
                     var filePath = obj.value;            
                     var fileSystem = new ActiveXObject("Scripting.FileSystemObject");   
                     var file = fileSystem.GetFile (filePath);               
                     fileSize = file.Size;         
                }else {  
                     fileSize = obj.files[0].size;     
                } 
                console.log(fileSize +"byte");
                fileSize=Math.round(fileSize/1024*100)/100; //单位为KB
                console.log(fileSize+"KB");
                
                if(fileSize>=maxsize){
                	rmsBox.notify('warning', {msg: "文件最大尺寸为"+maxsize+"KB，请重新上传！", delay:3000});
                    return false;
                }

                return true;
                
            }





			
		}
	};
}();